<?php

namespace App\Http\Controllers\Export;

use App\Http\Controllers\Controller;
use App\Models\UserInfo;
use Maatwebsite\Excel\Facades\Excel;

/**
 * excel导出导入功能  用户数据导出
 */
class UserInfoExport extends Controller
{

    public $model = null;
    public $limit = 99999;

    public function __construct()
    {
        parent::__construct();

        $this->model = new UserInfo();
    }


    /**
     * 用户列表导出
     * @param page 默认为 1
     * @param limit 限制条数 默认为 10
     * @param keywords 检索内容
     * @param start_time 开始时间
     * @param end_time 结束时间
     * @param sort 排序方式   1 默认排序（默认）  2 积分倒序
     */
    public function index()
    {
        $page = intval($this->request->page) ?: 1;
        $limit = $this->request->limit ?: 10;
        $keywords = $this->request->keywords;
        $start_time = $this->request->start_time;
        $end_time = $this->request->end_time;
        $sort = $this->request->input('sort', 1);

        $this->request->merge(['page' => 1]); //重置分页参数

        $data = $this->model->lists($keywords, $start_time, $end_time, $sort,  $this->limit);

        if ($data['total'] == 0 || !$data['data']) {
            return $this->returnApi(203, "暂无数据");
        }
        //设置表头
        $row = [[
            "id" => 'ID',
            "serial_number" => '用户编号',
            "tel" => '用户账号',
            "nickname" => '微信昵称',
            "score" => '积分',
            "invite_number" => '邀请量',
            "user_access_day" => '访问天数',
            "user_read_duration" => '阅读时长',
        ]];

        $title = '用户数据导出-' . date('YmdHis');
        //执行导出
        $data = $this->setData($data['data']); //要导入的数据
        return $this->exportData($data, $title, $row);
    }

    /**
     * 导出数据
     *
     * @param [type] $data
     * @return void
     */
    public function exportData($data, $title, $row)
    {
        $header = $row; //导出表头
        $excel = new Export($data, $header, $title);
        $excel->setColumnWidth(['A' => 10, 'B' => 30, 'C' => 30, 'D' => 20, 'E' => 50, 'F' => 20, 'G' => 20, 'H' => 20, 'I' => 20, 'J' => 20, 'K' => 20, 'L' => 30]);
        $excel->setRowHeight([1 => 30]);
        // $excel->setFreezePane('A2');
        // $excel->setFont(['A1:Z1265' => '宋体']);
        // $excel->setFontSize(['A1:I1' => 14,'A2:Z1265' => 10]);
        // $excel->setBold(['A1:Z2' => true]);
        // $excel->setBackground(['A1:A1' => '808080','C1:C1' => '708080']);
        // $excel->setMergeCells(['A1:I1']);
        // $excel->setBorders(['A2:D5' => '#000000']);
        return Excel::download($excel, $title . '.xlsx');
    }

    public function setData($data)
    {
        $excel_data = [];
        /*设置excel内容*/
        foreach ($data as $key => $val) {
            $excel_data[$key]['id'] = $key + 1;
            $excel_data[$key]['serial_number'] = $val['serial_number'];
            $excel_data[$key]['tel'] = $val['tel'];
            $excel_data[$key]['nickname'] = $val['nickname'];
            $excel_data[$key]['score'] = $val['score'];
            $excel_data[$key]['invite_number'] = $val['invite_number'];
            $excel_data[$key]['user_access_day'] = $val['user_access_day'];
            $excel_data[$key]['user_read_duration'] = $val['user_read_duration'];
        }
        return $excel_data;
    }
}
